package luzhin;

import java.util.*;

/**
 * Cache for a search. 
 * @author lacungus
 *
 */
public class SearchCache {
	private int minCacheDepth;
	private int maxCacheDepth;
	private HashMap<Board, CacheData> caches;
	
	/**
	 * 
	 * @param minCacheDepth Minimal depth to be kept in cache. 
	 */
	public SearchCache(int minCacheDepth, int maxCacheDepth) {
		this.minCacheDepth = minCacheDepth;
		this.maxCacheDepth = maxCacheDepth;
		caches = new HashMap<Board, CacheData>();
	}
	
	public void put(Board board, CacheData data) {
		if (data.getDepth() < minCacheDepth)
			return;
        CacheData containedCache = caches.get(board);        
        if (containedCache == null || containedCache.getDepth() < data.getDepth())
            caches.put(board, data);
	}
	
	public CacheData get(Board board, int depth) {
		CacheData res = caches.get(board);
        if (res == null || res.getDepth() < depth)
            return null;
		return res;
	}
	
}
